package component.controls
{
	import component.controls.ui.Component;
	import component.global.ComponentSetting;
	import component.global.utils.ObjectUtil;
	
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Label extends Component
	{
		public function Label($text:String = ComponentSetting.LABEL_DEFAULT_STRING, textFormat:TextFormat = null)
		{
			_labelStr = $text;
			_textFormat = textFormat;
			super();
		}
		
		protected var _textFormat:TextFormat;
		protected var _textField:TextField;
		protected var _labelStr:String;
		protected var _lineWidth:int;
		
		override protected function init():void{
			mouseEnabled = mouseChildren = false;
			_textField = new TextField();
			if(_textFormat == null){
				_textFormat = new TextFormat("Arial",12,null,null,null,null,null,null,"center");
			} else {
				embedFonts = true;
			}
			_textField.defaultTextFormat = _textFormat;
			_textField.text = _labelStr;
			_textField.width = _textField.textWidth + 4;
			_textField.height = 40;
			addChild(_textField);
		}
		
		public function get embedFonts():Boolean{
			return _textField.embedFonts;
		}
		
		public function set embedFonts(value:Boolean):void{
			_textField.embedFonts = true;
			updateSize();
		}
		
		public function set autoSize(value:String):void{
			_textField.autoSize = value;
		}
		
		public function set text(value:String):void{
			_textField.text = value;
			updateSize()
		}
		
		public function get text():String{
			return _textField.text;
		}
		
		public function set textFormat(text_format:TextFormat):void{
			_textField.setTextFormat(text_format);
			_textField.defaultTextFormat = _textFormat = text_format;
			updateSize();
		}
		
		public function set color(value:uint):void{
			_textField.textColor = value;
		}
		
		public function set lineWidth(value:int):void{
			_lineWidth = value;
		}
		
		private function updateSize():void{
			_textField.width = _textField.textWidth + 4;
			if(_lineWidth != 0){
					_textField.width = _lineWidth;
					_textField.multiline = true;
					_textField.wordWrap = true;
//				_textField.text = _textField.text;
			} 
			
			_textField.height = _textField.textHeight + Number(_textFormat.size)/2;
			if(_textField.height == _textField.textHeight){
				_textField.height = _textField.textHeight * 2;
			}
//			_textField.border = true;
//			_textField.borderColor = 0xFFFFFF;
		}
		
		override public function dispose():void{
			super.dispose();
			ObjectUtil.dispose(_textField);_textField=null;
			ObjectUtil.dispose(_textFormat);_textFormat=null;
		}
	}
}